
#include "StdAfx.h"
#include "MySQL_DBResultSet.h"
#include "MySQL_DBSession.h"

MySQL_DBResultSet::MySQL_DBResultSet(){
	result_set_ = 0;
	current_get_field_ = 0;
	row_ = 0;
	field_count_ = 0;
}

MySQL_DBResultSet::~MySQL_DBResultSet(){
}

int MySQL_DBResultSet::next(){	
	if(! result_set_ )
		return 0;

	row_ = mysql_fetch_row( result_set_ );
	
	if(! row_ ){
		mysql_free_result( result_set_ );
		result_set_ = 0;

		return 0;	
	}

	field_count_ = mysql_num_fields( result_set_ );

	current_get_field_ = 0;

	return field_count_;
}

void MySQL_DBResultSet::reset(){	
	current_get_field_ = 0;
	row_ = 0;

	if( result_set_ ){
		mysql_free_result( result_set_ );
		result_set_ = 0;
	}

	field_count_ = 0;	
}

size_t MySQL_DBResultSet::getAffectedCount(){
	return 0;
}
	
char MySQL_DBResultSet::getByte(){
	if( current_get_field_ >= field_count_ )
		return 0;

	return (char)*(row_[ current_get_field_++ ]);
}

short MySQL_DBResultSet::get2Byte(){
	if( current_get_field_ >= field_count_ )
		return 0;

	return (short)atoi( row_[ current_get_field_++ ] );
}

int MySQL_DBResultSet::get4Byte(){
	if( current_get_field_ >= field_count_ )
		return 0;

	return atoi( row_[ current_get_field_++ ] );
}

__int64 MySQL_DBResultSet::get8Byte(){
	if( current_get_field_ >= field_count_ )
		return 0;

#ifdef WIN32
	return _atoi64( row_[ current_get_field_++ ] );
#else
	return atoll( row_[ current_get_field_++ ] );
#endif		
}

int MySQL_DBResultSet::getString( char* buffer, size_t size ){
	if( current_get_field_ >= field_count_ )
		return 0;

	if(! buffer )
		return 0;

	memset( buffer, 0, size );
	char* target = row_[ current_get_field_++ ];

	strncpy( buffer, target, size );
	return size;
}

int MySQL_DBResultSet::getMemory( char* buffer, size_t size ){
	if( current_get_field_ >= field_count_ )
		return 0;

	if(! buffer )
		return 0;

	memset( buffer, 0, size );
	char* target = row_[ current_get_field_++ ];

	memcpy( buffer, target, size );
	return size;
}

double MySQL_DBResultSet::getFloat(){
	return atof( row_[ current_get_field_++ ] );
}

int MySQL_DBResultSet::get_resultset_desc( void* handle, DBTableDescription& table_desc ){
	return 0;
}